﻿using System;
using System.Collections.Generic;
using WebAPI.Dao;
using WebAPI.Entities;

namespace WebAPI.Services;

public class ArticleService
{
    private ArticleMapper _articleMapper = new ArticleMapper();
    private TimeTransferUtils _transferUtils = new TimeTransferUtils();
    private IdGenerate _idGenerate = new IdGenerate();
    
    public List<Article> GetAllArticles()
    {
        List<Article> articles = _articleMapper.SelectAll();
        
        return articles;
    }
    
    public List<Article> GetAllArticles(string userId)
    {
        List<Article> articles = _articleMapper.SelectAll(userId);
        
        return articles;
    }
    
    public Article GetOneArticles(String _id)
    {
        Article article = _articleMapper.SelectOne(_id);
        
        return article;
    }

    public bool PublicArticle(Article article)
    {
        article.Id = _idGenerate.GetId();
        article.CreateTime = TimeTransferUtils.GetSqlServerTimeString();
        article.UpdateTime = TimeTransferUtils.GetSqlServerTimeString();
        int flag = _articleMapper.Insert(article);
        
        return flag > 0;
    }
    
    public bool UpdateArticle(Article article)
    {
        article.UpdateTime = TimeTransferUtils.GetSqlServerTimeString();
        int flag = _articleMapper.Update(article);
        
        return flag > 0;
    }
    
    public bool DeleteArticle(Article article)
    {
        int flag = _articleMapper.Delete(article);
        
        return flag > 0;
    }
}